package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.DestinationQuery;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.sun.javafx.scene.traversal.TraverseListener;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IDestinationService destinationService;
    @Reference
    private IRegionService regionService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model) {

        //hotRegions    热门区域
        model.addAttribute("hotRegions", regionService.queryHotRegions());

        return "destination/index";
    }


    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model, @ModelAttribute("regionId") Long regionId) {
        //查询所有热门目的地
        List<Destination> dest = destinationService.getDestByRegionId(regionId);
        //leftDests
        model.addAttribute("leftDests", dest.subList(0, dest.size() >> 1));
        //rightDests
        model.addAttribute("rightDests", dest.subList(dest.size() >> 1, dest.size()));
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Model model, Long id) {

        //toasts    吐丝查询
        List<Destination> toasts = destinationService.getToasts(id);

        Destination destination = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);

        //catalogs  目的地下的攻略分类
        model.addAttribute("catalogs", strategyCatalogService.getByDestId(id));

        //strategyDetails   当前目的地下点击量前三的攻略集合
        model.addAttribute("strategyDetails",strategyDetailService.listViewnumTop3(id));

        return "destination/guide";
    }


    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {

        //toasts    吐丝查询
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        Destination destination = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);

        return "destination/survey";
    }


    @RequestMapping("/survey")
    public String survey(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo) {

        //catalogs  该目的地下的所有分类
        List<StrategyCatalog> catalogs = strategyCatalogService.getByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        //catalog   当前选中的分类
        if (catalogs.size()>0){
            for (StrategyCatalog catalog : catalogs) {
                if (catalog.getId()==qo.getCatalogId()){
                    model.addAttribute("catalog",catalog);
                    //detail    该分类下第一篇文章
                    List<StrategyDetail> details = catalog.getDetails();
                    if (details.size()>0){
                        StrategyDetail detail = details.get(0);
                        StrategyContent content = strategyDetailService.getContent(detail.getId());
                        detail.setStrategyContent(content);
                        model.addAttribute("detail",detail);
                    }
                }
            }
        }

        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo){

        //pageInfo  分页信息
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";

    }




}


